package chamster.maeander;

import java.util.HashMap;
import java.util.List;

import chamster.MaeanderHamster;
import chamster.TurnBackAction;
import chamster.actions.ForewardAction;
import chamster.actions.HamsterAction;
import chamster.actions.MoveLeftAction;
import chamster.actions.MoveRightAction;
import chamster.random.RandomStatelessActionMachine;
import chamster.random.StaticRandomMachine;
import chamster.statemachine.HamsterState;

public class RandomState implements MaeanderState {

	private final MaeanderState nextState;
	private final RandomStatelessActionMachine<MaeanderHamster> decisionMaker;

	
	public RandomState(int randomSpan, MaeanderState nextState) {
		if(randomSpan <= 1) {
			this.nextState = nextState;
		}
		else {
			this.nextState = new RandomState(randomSpan - 1, nextState);
		}
		HashMap<HamsterAction<MaeanderHamster>, Integer> actions = 
				new HashMap<HamsterAction<MaeanderHamster>, Integer>();
		actions.put(new ForewardAction<MaeanderHamster>(), 10);
		actions.put(new MoveLeftAction<MaeanderHamster>(), 10);
		actions.put(new MoveRightAction<MaeanderHamster>(), 10);
		actions.put(new TurnBackAction<MaeanderHamster>(), 1);
		this.decisionMaker = new StaticRandomMachine<MaeanderHamster>(actions);
	}
	
	public List<HamsterState<MaeanderHamster>> applyState(
			List<HamsterState<MaeanderHamster>> states, MaeanderHamster hamster)
			throws Exception {
		this.decisionMaker.decideAndExecute(hamster);
		if(this.nextState.isApplicable(hamster)) {
			states.add(this.nextState);
		}
		else {
			states.add(this);
		}
		return states;
	}

	public boolean isApplicable(MaeanderHamster hamster) {
		return true;
	}

}
